package com.yunze.web.controller.yunze.card;

import com.alibaba.fastjson.JSON;
import com.yunze.common.annotation.Log;
import com.yunze.common.core.domain.entity.SysUser;
import com.yunze.common.core.domain.model.LoginUser;
import com.yunze.common.enums.BusinessType;
import com.yunze.common.utils.ServletUtils;
import com.yunze.common.utils.ip.IpUtils;
import com.yunze.common.utils.spring.SpringUtils;
import com.yunze.common.utils.yunze.AesEncryptUtil;
import com.yunze.framework.web.service.TokenService;
import com.yunze.system.service.impl.yunze.YzSmsLogServiceImpl;
import com.yunze.system.service.yunze.IYzCardPackageService;
import com.yunze.system.service.yunze.IYzSmsLogService;
import com.yunze.web.core.config.MyBaseController;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 短信日志
 * 2022年9月2日10:57:25
 */
@Api("短信日志")
@RestController
@RequestMapping("/yunze/smslog")
@Slf4j
public class YzSmsLogController extends MyBaseController {

    @Autowired
    private IYzSmsLogService iYzSmsLogService;

    /**
     * 获取展示在首页的数据
     * 2022年9月2日10:59:49
     */
    @PostMapping(value = "/list", produces = { "application/json;charset=UTF-8" })
    @PreAuthorize("@ss.hasPermi('yunze:sysLog:list')")
    public String getList(@RequestBody String Pstr){

        HashMap<String, Object> Parammap = new HashMap<String, Object>();
        if(Pstr!=null){
            Pstr = Pstr.replace("%2F", "/");//转义 /
        }
        try {
            Pstr =  AesEncryptUtil.desEncrypt(Pstr);
            Parammap.putAll(JSON.parseObject((String) Pstr));
            LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
            SysUser currentUser = loginUser.getUser();
            List<Integer> agent_id = new ArrayList<>();
            String str_dept_id = currentUser.getDeptId().toString();
            Integer int_agent_id = Integer.parseInt(str_dept_id);
            if (currentUser.getDeptId() != 100) {
                if (Parammap.get("agent_id") != null) {
                    List<Integer> P_agent_id = (List<Integer>) Parammap.get("agent_id");
                    agent_id.addAll(P_agent_id);
                } else {
                    agent_id.add(int_agent_id);
                    Parammap.put("agent_id", agent_id);
                }
            }

            return MyRetunSuccess(iYzSmsLogService.getList(Parammap),null);
        }catch (Exception e){
            String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
            logger.error("<br/> yunze:tariffGroup:list  <br/> Pstr = " + Pstr + " <br/> ip =  " + ip + " <br/> ",e.getCause().toString());
        }

        return Myerr("获取短信日志列表 操作失败!");
    }


}
